package hu.ikti.oamadmin.utlis;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {

	public static byte[] getBytesFromURL(URL url) throws IOException {

		URLConnection urlConnection = url.openConnection();

		InputStream is = urlConnection.getInputStream();
		// Create the byte array to hold the data
		byte[] bytes = new byte[(int) urlConnection.getContentLength()];

		// Read in the bytes
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}

		// Ensure all the bytes have been read in
		if (offset < bytes.length) {
			throw new IOException("Could not completely read url " + url);
		}

		// Close the input stream and return bytes
		is.close();
		return bytes;
	}

}
